﻿using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;

namespace O1.Data
{
    [ContractVerification(true)]
    internal static class EdgeExtensions
    {
        public static Guid ToFingerprint(this Edge edge, CellAddress startVertex, EdgeDirections direction)
        {
            return new Fingerprint(edge, startVertex, direction).Value;
        }

        [StructLayout(LayoutKind.Explicit)]
        private struct Fingerprint
        {
            [FieldOffset(0)]
            public readonly Guid Value;

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "Union struct. Value is used.")]
            [FieldOffset(0)]
            private readonly CellAddress startVertex;

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "Union struct. Value is used.")]
            [FieldOffset(6)]
            private readonly byte zero;

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "Union struct. Value is used.")]
            [FieldOffset(7)]
            private readonly byte direction;

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "Union struct. Value is used.")]
            [FieldOffset(8)]
            private readonly long edge;

            public Fingerprint(Edge edge, CellAddress startVertex, EdgeDirections direction)
                : this()
            {
                this.startVertex = startVertex;
                this.direction = (byte)direction;
                this.edge = edge;
            }
        }
    }
}